//
//  NetworkCommand.swift
//  TJJPT
//
//  Created by FENGYL on 2017/5/10.
//  Copyright © 2017年 FENGYL. All rights reserved.
//

import UIKit
import Alamofire

class NetworkCommand: NSObject {
    
    static let shareNetworkCommand = NetworkCommand()
    
    // MARK: - 查询账号名是否已经注册(全部返回null,没卵用)
    ///查询账号名是否已经注册(全部返回null,没卵用)
    class func existAccountName(userName: String, success: @escaping (_ responseObject: [String:Any]) -> (), failure: @escaping (_ error: Error) -> ()){
        
        var parameters = [String:Any]()
        
        parameters.updateValue(userName, forKey: NETWORK_COMMAND_KEY_USER)
        
        let urlString:String = NETWORK_COMMAND_URL_SERVER_URL + NETWORK_COMMAND_URL_EXIST_ACCOUNT_NAME
        
        print("urlString: \(urlString), parameters: \(parameters)")
        
        
        
        // MARK: 请求
        Alamofire.request(urlString, method: .post, parameters: parameters).responseJSON { response in
            
            switch response.result{
                
                case .success(let value):
                    
                    print(value)
                
                    success(value as! [String : Any])
                
                    break
                
                case .failure(let value):
                    
                    print(failure)
                    
                    failure(value)
                
                    break
                
                
            }
            
        }
        
    }
    
    
    // MARK: - 登陆API
    ///登陆API
    class func login(userName:String,password:String, success: @escaping (_ responseObject: [String:Any]) -> (), failure: @escaping (_ error: Error) -> ()) {
        
        var parameters = [String:Any]()
        
        let md5Password = ("HYZ" + userName + password).md5()
        
        
        //post内容
        parameters.updateValue(UUID().uuidString, forKey: NETWORK_COMMAND_KEY_GUID)
        parameters.updateValue(STATIC_VALUE_TERMINAL_MODEL, forKey: NETWORK_COMMAND_KEY_TERMINAL_MODEL)
        parameters.updateValue(STATIC_VALUE_PLATFORM, forKey: NETWORK_COMMAND_KEY_PLAT_FORM)
        parameters.updateValue(md5Password, forKey: NETWORK_COMMAND_KEY_PASSWORD)
        parameters.updateValue(3, forKey: NETWORK_COMMAND_KEY_ACCOUNT_TYPE)
        parameters.updateValue(userName, forKey: NETWORK_COMMAND_KEY_USER)
        
        
        
        let urlString:String = NETWORK_COMMAND_URL_SERVER_URL + NETWORK_COMMAND_URL_LOGON
        
        print("urlString: \(urlString), parameters: \(parameters)")
        
        
        
        // MARK: 请求
        Alamofire.request(urlString, method: .post, parameters: parameters).responseJSON { response in
            
            switch response.result{
                
            case .success(let value):
                
                print(value)
                
                
                // MARK: 判断是否登录成功
                let responseObject : [String : Any] = value as! [String : Any]
                
                let id:NSInteger = responseObject[NETWORK_COMMAND_KEY_ID] as! NSInteger
                
                if id <= 0{
                    
                    let error = NSError()
                    
                    failure(error)
                    
                    break
                    
                }
                
                // MARK: 相关信息存到model里
                UserModel.userModel.id = responseObject[NETWORK_COMMAND_KEY_ID] as? NSNumber
                
                UserModel.userModel.clientType = responseObject[NETWORK_COMMAND_KEY_CLIENT_TYPE] as? NSNumber
                
                UserModel.userModel.tokenPass = responseObject[NETWORK_COMMAND_KEY_TOKEN_PASS] as? String
                
                UserModel.userModel.userName = responseObject[NETWORK_COMMAND_KEY_USER_NAME] as? String
                
                
                // MARK: 
                UserDefaults.standard.set(userName, forKey: USER_DEFAULTS_USER_NAME)
                
                UserDefaults.standard.set(password, forKey: USER_DEFAULTS_PASSWORD)
                
                success(value as! [String : Any])
                
                break
                
            case .failure(let value):
                
                print(failure)
                
                failure(value)
                
                break
                
                
            }
            
        }
        
        
    }
    
    // MARK: - 创建账号
    ///创建账号
    class func createNetAccount(userName:String,password:String, success: @escaping (_ responseObject: [String:Any]) -> (), failure: @escaping (_ error: Error) -> ()) {
        
        var parameters = [String:Any]()
        
        let md5Password = ("HYZ" + userName + password).md5()
        
        //post内容
        parameters.updateValue(md5Password, forKey: NETWORK_COMMAND_KEY_PASSWORD)
        parameters.updateValue(userName, forKey: NETWORK_COMMAND_KEY_USER)
        
        
        
        let urlString:String = NETWORK_COMMAND_URL_SERVER_URL + NETWORK_COMMAND_URL_LOGON
        
        print("urlString: \(urlString), parameters: \(parameters)")
        
        
        
        //请求
        Alamofire.request(urlString, method: .post, parameters: parameters).responseJSON { response in
            
            switch response.result{
                
            case .success(let value):
                
                print(value)
                
                success(value as! [String : Any])
                
                break
                
            case .failure(let value):
                
                print(failure)
                
                failure(value)
                
                break
                
                
            }
            
        }
        
        
    }
    
    // MARK: - 获取指定账号下的所有设备
    ///获取指定账号下的所有设备
    class func GetDevicesByAccount(id:NSNumber?, clientType:NSNumber?, tokenPass:String?, success: @escaping (_ responseObject: Any) -> (), failure: @escaping (_ error: Error) -> ())  {
        
        var parameters = [String:Any]()
        
        //post内容
        //id
        var postId:NSNumber = 0
        
        if id != nil {
            
            postId = id!
            
        }else if UserModel.userModel.id != nil{
            
            postId = UserModel.userModel.id!
            
        }
        
        parameters.updateValue(postId, forKey: NETWORK_COMMAND_KEY_ID)
        
        
        
        //clientType
        var postClientType:NSNumber = 0
        
        if clientType != nil {
            
            postClientType = clientType!
            
        }else if UserModel.userModel.clientType != nil{
            
            postClientType = UserModel.userModel.clientType!
            
        }
        
        parameters.updateValue(postClientType, forKey: NETWORK_COMMAND_KEY_CLIENT_TYPE)
        
        
        
        //tokenPass
        var postTokenPass = ""
        
        if tokenPass != nil {
            
            postTokenPass = tokenPass!
            
        }else if UserModel.userModel.tokenPass != nil{
            
            postTokenPass = UserModel.userModel.tokenPass!
            
        }
        
        parameters.updateValue(postTokenPass, forKey: NETWORK_COMMAND_KEY_TOKEN_PASS)
        
        
        
        
        let urlString:String = NETWORK_COMMAND_URL_SERVER_URL + NETWORK_COMMAND_URL_GET_DEVICES_BY_ACCOUNT
        
        print("urlString: \(urlString), parameters: \(parameters)")
        
        
        
        //请求
        Alamofire.request(urlString, method: .post, parameters: parameters).responseJSON { response in
            
            switch response.result{
                
            case .success(let value):
                
                print(value)
                
                UserModel.userModel.devicesByAccountArray = value as? NSArray
                
                success(value)
                
                break
                
            case .failure(let value):
                
                print(failure)
                
                failure(value)
                
                break
                
                
            }
            
        }
        
    }
    
    // MARK: - 批量获取最后状态信息
    ///批量获取最后状态信息
    class func getLastStatusData(listSerialNumber:String?, id:NSNumber?, clientType:NSNumber?, tokenPass:String?, success: @escaping (_ responseObject: Any) -> (), failure: @escaping (_ error: Error) -> ()) {
        
        var parameters = [String:Any]()
        
        //post内容
        //listSerialNumber
        if listSerialNumber == nil {
            
            let list:String = self.getAllDeviceSerialNumberListString()
            
            parameters.updateValue(list, forKey: NETWORK_COMMAND_KEY_LIST_SERIAL_NUMBER)
            
        }else{
            
            parameters.updateValue(listSerialNumber!, forKey: NETWORK_COMMAND_KEY_LIST_SERIAL_NUMBER)
            
        }
        
        //id
        var postId:NSNumber = 0
        
        if id != nil {
            
            postId = id!
            
        }else if UserModel.userModel.id != nil{
            
            postId = UserModel.userModel.id!
            
        }
        
        parameters.updateValue(postId, forKey: NETWORK_COMMAND_KEY_ID)
        
        
        
        //clientType
        var postClientType:NSNumber = 0
        
        if clientType != nil {
            
            postClientType = clientType!
            
        }else if UserModel.userModel.clientType != nil{
            
            postClientType = UserModel.userModel.clientType!
            
        }
        
        parameters.updateValue(postClientType, forKey: NETWORK_COMMAND_KEY_CLIENT_TYPE)
        
        
        
        //tokenPass
        var postTokenPass = ""
        
        if tokenPass != nil {
            
            postTokenPass = tokenPass!
            
        }else if UserModel.userModel.tokenPass != nil{
            
            postTokenPass = UserModel.userModel.tokenPass!
            
        }
        
        parameters.updateValue(postTokenPass, forKey: NETWORK_COMMAND_KEY_TOKEN_PASS)
        
        
        
        
        let urlString:String = NETWORK_COMMAND_URL_SERVER_URL + NETWORK_COMMAND_URL_GET_LAST_STATUS_DATA
        
        print("urlString: \(urlString), parameters: \(parameters)")
        
        //请求
        Alamofire.request(urlString, method: .post, parameters: parameters).responseJSON { response in
            
            switch response.result{
                
            case .success(let value):
                
                print(value)
                
                UserModel.userModel.lastStatusDataArray = value as? NSArray
                
                success(value)
                
                break
                
            case .failure(let value):
                
                print(failure)
                
                failure(value)
                
                break
                
                
            }
            
        }

        
    }
    
    // MARK: - 修改特定设备的属性信息
    ///修改特定设备的属性信息
    class func updateDeviceAttribute(serialNumber:String?, id:NSNumber?, clientType:NSNumber?, tokenPass:String?, iconUrl:String?, driver:String?, licensePlate:String?, displayName:String?, success: @escaping (_ responseObject: Any) -> (), failure: @escaping (_ error: Error) -> ()) {
        
        var parameters = [String:Any]()
        
        //post内容
        //SerialNumber
        var postSerialNumber = ""
        
        if serialNumber != nil {
            
            postSerialNumber = serialNumber!
            
        }else{
            
            postSerialNumber = UserModel.userModel.selectedDeviceSerialNumber
            
        }
        
        parameters.updateValue(postSerialNumber, forKey: NETWORK_COMMAND_KEY_LIST_SERIAL_NUMBER)
        
        
        
        
        //displayName
        if displayName == nil {
            
            return
            
        }else{
            
            parameters.updateValue(displayName!, forKey: NETWORK_COMMAND_KEY_DISPLAY_NAME)
            
        }
        
        
        
        //id
        var postId:NSNumber = 0
        
        if id != nil {
            
            postId = id!
            
        }else if UserModel.userModel.id != nil{
            
            postId = UserModel.userModel.id!
            
        }
        
        parameters.updateValue(postId, forKey: NETWORK_COMMAND_KEY_ID)
        
        
        
        //clientType
        var postClientType:NSNumber = 0
        
        if clientType != nil {
            
            postClientType = clientType!
            
        }else if UserModel.userModel.clientType != nil{
            
            postClientType = UserModel.userModel.clientType!
            
        }
        
        parameters.updateValue(postClientType, forKey: NETWORK_COMMAND_KEY_CLIENT_TYPE)
        
        
        
        //tokenPass
        var postTokenPass = ""
        
        if tokenPass != nil {
            
            postTokenPass = tokenPass!
            
        }else if UserModel.userModel.tokenPass != nil{
            
            postTokenPass = UserModel.userModel.tokenPass!
            
        }
        
        parameters.updateValue(postTokenPass, forKey: NETWORK_COMMAND_KEY_TOKEN_PASS)
        
        
        
        //IconUrl
        if iconUrl != nil {
            
            parameters.updateValue(iconUrl!, forKey: NETWORK_COMMAND_KEY_ICON_URL)
            
        }
        
        //driver
        if driver != nil {
            
            parameters.updateValue(driver!, forKey: NETWORK_COMMAND_KEY_DRIVER)
            
        }
        
        //licensePlate
        if licensePlate != nil {
            
            parameters.updateValue(licensePlate!, forKey: NETWORK_COMMAND_KEY_LICENSE_PLATE)
            
        }
        
        let urlString:String = NETWORK_COMMAND_URL_SERVER_URL + NETWORK_COMMAND_URL_UPDATE_DEVICE_ATTRIBUTE
        
        print("urlString: \(urlString), parameters: \(parameters)")
        
        //请求
        Alamofire.request(urlString, method: .post, parameters: parameters).responseJSON { response in
            
            switch response.result{
                
            case .success(let value):
                
                print(value)
                
                let responseDict:NSDictionary = value as! NSDictionary
                
                let code:NSNumber = responseDict.value(forKey: NETWORK_COMMAND_KEY_CODE) as! NSNumber
                
                if code != 1 {
                    
                    failure(NSError())
                    
                    break
                    
                }
                
                NetworkCommand.GetDevicesByAccount(id: nil, clientType: nil, tokenPass: nil, success: { (response) in
                    
                    success(value)
                    
                }, failure: { (Error) in
                    
                })
                
                break
                
            case .failure(let value):
                
                print(failure)
                
                failure(value)
                
                break
                
                
            }
            
        }
        
    }
    
    // MARK: - 获取多个设备的最后数据点
    ///获取多个设备的最后数据点
    class func getLastTrackPoiDataBySns(listSerialNumber:String?, id:NSNumber?, clientType:NSNumber?, tokenPass:String?, success: @escaping (_ responseObject: Any) -> (), failure: @escaping (_ error: Error) -> ()) {
        
        var parameters = [String:Any]()
        
        //post内容
        //listSerialNumber
        if listSerialNumber == nil {
            
            let list:String = self.getAllDeviceSerialNumberListString()
            
            parameters.updateValue(list, forKey: NETWORK_COMMAND_KEY_LIST_SERIAL_NUMBER)
            
        }else{
            
            parameters.updateValue(listSerialNumber!, forKey: NETWORK_COMMAND_KEY_LIST_SERIAL_NUMBER)
            
        }
        
        //id
        var postId:NSNumber = 0
        
        if id != nil {
            
            postId = id!
            
        }else if UserModel.userModel.id != nil{
            
            postId = UserModel.userModel.id!
            
        }
        
        parameters.updateValue(postId, forKey: NETWORK_COMMAND_KEY_ID)
        
        
        
        //clientType
        var postClientType:NSNumber = 0
        
        if clientType != nil {
            
            postClientType = clientType!
            
        }else if UserModel.userModel.clientType != nil{
            
            postClientType = UserModel.userModel.clientType!
            
        }
        
        parameters.updateValue(postClientType, forKey: NETWORK_COMMAND_KEY_CLIENT_TYPE)
        
        
        
        //tokenPass
        var postTokenPass = ""
        
        if tokenPass != nil {
            
            postTokenPass = tokenPass!
            
        }else if UserModel.userModel.tokenPass != nil{
            
            postTokenPass = UserModel.userModel.tokenPass!
            
        }
        
        parameters.updateValue(postTokenPass, forKey: NETWORK_COMMAND_KEY_TOKEN_PASS)
        
        
        
        
        let urlString:String = NETWORK_COMMAND_URL_SERVER_URL + NETWORK_COMMAND_URL_GET_LAST_TRACK_POI_DATA_BY_SNS
        
        print("urlString: \(urlString), parameters: \(parameters)")
        
        //请求
        Alamofire.request(urlString, method: .post, parameters: parameters).responseJSON { response in
            
            switch response.result{
                
            case .success(let value):
                
                print(value)
                
                UserModel.userModel.lastTrackPoiDataBySnsArray = value as? NSArray
                
                success(value)
                
                break
                
            case .failure(let value):
                
                print(failure)
                
                failure(value)
                
                break
                
                
            }
            
        }
        
    }
    
    // MARK: - 获取时间段内的报警数据
    ///获取时间段内的报警数据
    class func getHistoryEventDataByTime(selectDate:Date?, serialNumber:String?, id:NSNumber?, clientType:NSNumber?, tokenPass:String?, success: @escaping (_ responseObject: Any) -> (), failure: @escaping (_ error: Error) -> ()) {
        
        var parameters = [String:Any]()
        
        //post内容
        //StartTime
        if selectDate != nil {
        
            let dateFormatter = DateFormatter.init()
            dateFormatter.dateFormat = STATIC_VALUE_DATE_FORMATTER
            
            let dateString = dateFormatter.string(from: selectDate!) + " 00:00:00"
            
            
            
            let newDateFormatter = DateFormatter.init()
            
            newDateFormatter.dateFormat = STATIC_VALUE_DATE_FORMATTER + " " + STATIC_VALUE_TIME_FORMATTER
            
            let newDate = newDateFormatter.date(from: dateString)
            
            let dateNumber = NSNumber.init(value: (newDate?.timeIntervalSince1970)!)

            parameters.updateValue(dateNumber, forKey: NETWORK_COMMAND_KEY_START_TIME)
            
        }
        
        //EndTime
        if selectDate != nil {
            
            let dateFormatter = DateFormatter.init()
            dateFormatter.dateFormat = STATIC_VALUE_DATE_FORMATTER
            
            let dateString = dateFormatter.string(from: selectDate!) + " 23:59:59"
            
            
            
            let newDateFormatter = DateFormatter.init()
            
            newDateFormatter.dateFormat = STATIC_VALUE_DATE_FORMATTER + " " + STATIC_VALUE_TIME_FORMATTER
            
            let newDate = newDateFormatter.date(from: dateString)
            
            let dateNumber = NSNumber.init(value: (newDate?.timeIntervalSince1970)!)
            
            parameters.updateValue(dateNumber, forKey: NETWORK_COMMAND_KEY_END_TIME)
            
        }
        
        
        //SerialNumber
        var postSerialNumber = ""
        
        if serialNumber != nil {
            
            postSerialNumber = serialNumber!
            
        }else{
            
            postSerialNumber = UserModel.userModel.selectedDeviceSerialNumber
            
        }
        
        parameters.updateValue(postSerialNumber, forKey: NETWORK_COMMAND_KEY_SERIAL_NUMBER)
        
        
        //id
        var postId:NSNumber = 0
        
        if id != nil {
            
            postId = id!
            
        }else if UserModel.userModel.id != nil{
            
            postId = UserModel.userModel.id!
            
        }
        
        parameters.updateValue(postId, forKey: NETWORK_COMMAND_KEY_ID)
        
        
        
        //clientType
        var postClientType:NSNumber = 0
        
        if clientType != nil {
            
            postClientType = clientType!
            
        }else if UserModel.userModel.clientType != nil{
            
            postClientType = UserModel.userModel.clientType!
            
        }
        
        parameters.updateValue(postClientType, forKey: NETWORK_COMMAND_KEY_CLIENT_TYPE)
        
        
        
        //tokenPass
        var postTokenPass = ""
        
        if tokenPass != nil {
            
            postTokenPass = tokenPass!
            
        }else if UserModel.userModel.tokenPass != nil{
            
            postTokenPass = UserModel.userModel.tokenPass!
            
        }
        
        parameters.updateValue(postTokenPass, forKey: NETWORK_COMMAND_KEY_TOKEN_PASS)
        
        
        
        
        
        let urlString:String = NETWORK_COMMAND_URL_SERVER_URL + NETWORK_COMMAND_URL_GET_HISTORY_EVENT_DATA_BY_TIME
        
        print("urlString: \(urlString), parameters: \(parameters)")
        
        //请求
        Alamofire.request(urlString, method: .post, parameters: parameters).responseJSON { response in
            
            switch response.result{
                
            case .success(let value):
                
                print(value)
                
                success(value)
                
                break
                
            case .failure(let value):
                
                print(failure)
                
                failure(value)
                
                break
                
                
            }
            
        }

        
    }
    
    // MARK: - 跟据时间获取历史报警(弃用)(Hide)(Delete) 下个版本将放弃
    ///跟据时间获取历史报警(弃用)(Hide)(Delete) 下个版本将放弃
    class func getHistoryAlarmDataByTime(selectDate:Date?, serialNumber:String?, id:NSNumber?, clientType:NSNumber?, tokenPass:String?, success: @escaping (_ responseObject: Any) -> (), failure: @escaping (_ error: Error) -> ()) {
        
        var parameters = [String:Any]()
        
        //post内容
        //StartTime
        if selectDate != nil {
            
            let dateFormatter = DateFormatter.init()
            dateFormatter.dateFormat = STATIC_VALUE_DATE_FORMATTER
            
            let dateString = dateFormatter.string(from: selectDate!) + " 00:00:00"
            
            
            
            let newDateFormatter = DateFormatter.init()
            
            newDateFormatter.dateFormat = STATIC_VALUE_DATE_FORMATTER + " " + STATIC_VALUE_TIME_FORMATTER
            
            let newDate = newDateFormatter.date(from: dateString)
            
            let dateNumber = NSNumber.init(value: (newDate?.timeIntervalSince1970)!)
            
            parameters.updateValue(dateNumber, forKey: NETWORK_COMMAND_KEY_START_TIME)
            
        }
        
        //EndTime
        if selectDate != nil {
            
            let dateFormatter = DateFormatter.init()
            dateFormatter.dateFormat = STATIC_VALUE_DATE_FORMATTER
            
            let dateString = dateFormatter.string(from: selectDate!) + " 23:59:59"
            
            
            
            let newDateFormatter = DateFormatter.init()
            
            newDateFormatter.dateFormat = STATIC_VALUE_DATE_FORMATTER + " " + STATIC_VALUE_TIME_FORMATTER
            
            let newDate = newDateFormatter.date(from: dateString)
            
            let dateNumber = NSNumber.init(value: (newDate?.timeIntervalSince1970)!)
            
            parameters.updateValue(dateNumber, forKey: NETWORK_COMMAND_KEY_END_TIME)
            
        }
        
        
        //SerialNumber
        var postSerialNumber = ""
        
        if serialNumber != nil {
            
            postSerialNumber = serialNumber!
            
        }else{
            
            postSerialNumber = UserModel.userModel.selectedDeviceSerialNumber
            
        }
        
        parameters.updateValue(postSerialNumber, forKey: NETWORK_COMMAND_KEY_SERIAL_NUMBER)
        
        
        //id
        var postId:NSNumber = 0
        
        if id != nil {
            
            postId = id!
            
        }else if UserModel.userModel.id != nil{
            
            postId = UserModel.userModel.id!
            
        }
        
        parameters.updateValue(postId, forKey: NETWORK_COMMAND_KEY_ID)
        
        
        
        //clientType
        var postClientType:NSNumber = 0
        
        if clientType != nil {
            
            postClientType = clientType!
            
        }else if UserModel.userModel.clientType != nil{
            
            postClientType = UserModel.userModel.clientType!
            
        }
        
        parameters.updateValue(postClientType, forKey: NETWORK_COMMAND_KEY_CLIENT_TYPE)
        
        
        
        //tokenPass
        var postTokenPass = ""
        
        if tokenPass != nil {
            
            postTokenPass = tokenPass!
            
        }else if UserModel.userModel.tokenPass != nil{
            
            postTokenPass = UserModel.userModel.tokenPass!
            
        }
        
        parameters.updateValue(postTokenPass, forKey: NETWORK_COMMAND_KEY_TOKEN_PASS)
        
        
        
        
        
        let urlString:String = NETWORK_COMMAND_URL_SERVER_URL + NETWORK_COMMAND_URL_GET_HISTORY_ALARM_DATA_BY_TIME
        
        print("urlString: \(urlString), parameters: \(parameters)")
        
        //请求
        Alamofire.request(urlString, method: .post, parameters: parameters).responseJSON { response in
            
            switch response.result{
                
            case .success(let value):
                
                print(value)
                
                success(value)
                
                break
                
            case .failure(let value):
                
                print(failure)
                
                failure(value)
                
                break
                
                
            }
            
        }
        
    }

    
    // MARK: - 获取单个设备的时间段数据
    ///获取单个设备的时间段数据
    class func getHistoryTrackPoiDataByTime(startTime:NSNumber, endTime:NSNumber, serialNumber:String?, id:NSNumber?, clientType:NSNumber?, tokenPass:String?, success: @escaping (_ responseObject: Any) -> (), failure: @escaping (_ error: Error) -> ()) {
        
        var parameters = [String:Any]()
        
        //post内容
        //stratTime
        parameters.updateValue(startTime, forKey: NETWORK_COMMAND_KEY_START_TIME)

        

        //endTime
        parameters.updateValue(endTime, forKey: NETWORK_COMMAND_KEY_END_TIME)

        
        //SerialNumber
        var postSerialNumber = ""
        
        if serialNumber != nil {
            
            postSerialNumber = serialNumber!
            
        }else{
            
            postSerialNumber = UserModel.userModel.selectedDeviceSerialNumber
            
        }
        
        parameters.updateValue(postSerialNumber, forKey: NETWORK_COMMAND_KEY_SERIAL_NUMBER)
        
        
        //id
        var postId:NSNumber = 0
        
        if id != nil {
            
            postId = id!
            
        }else if UserModel.userModel.id != nil{
            
            postId = UserModel.userModel.id!
            
        }
        
        parameters.updateValue(postId, forKey: NETWORK_COMMAND_KEY_ID)
        
        
        
        //clientType
        var postClientType:NSNumber = 0
        
        if clientType != nil {
            
            postClientType = clientType!
            
        }else if UserModel.userModel.clientType != nil{
            
            postClientType = UserModel.userModel.clientType!
            
        }
        
        parameters.updateValue(postClientType, forKey: NETWORK_COMMAND_KEY_CLIENT_TYPE)
        
        
        
        //tokenPass
        var postTokenPass = ""
        
        if tokenPass != nil {
            
            postTokenPass = tokenPass!
            
        }else if UserModel.userModel.tokenPass != nil{
            
            postTokenPass = UserModel.userModel.tokenPass!
            
        }
        
        parameters.updateValue(postTokenPass, forKey: NETWORK_COMMAND_KEY_TOKEN_PASS)
        
        
        
        
        
        let urlString:String = NETWORK_COMMAND_URL_SERVER_URL + NETWORK_COMMAND_URL_GET_HISTORY_TRACK_POI_DATA_BY_TIME
        
        print("urlString: \(urlString), parameters: \(parameters)")
        
        //请求
        Alamofire.request(urlString, method: .post, parameters: parameters).responseJSON { response in
            
            switch response.result{
                
            case .success(let value):
                
                print(value)
                
                success(value)
                
                break
                
            case .failure(let value):
                
                print(failure)
                
                failure(value)
                
                break
                
                
            }
            
        }
        
    }
    
    // MARK: - 发送指令(带参数)(OLD) 只支持一台设备(Hide)
    ///发送指令(带参数)(OLD) 只支持一台设备(Hide)
    class func putNetCommand(cmdName:String?, cmdValue:String?, msgId:String?,sendType:NSNumber?, serialNumber:String?, id:NSNumber?, clientType:NSNumber?, tokenPass:String?, success: @escaping (_ responseObject: Any) -> (), failure: @escaping (_ error: Error) -> ()) {
        
        var parameters = [String:Any]()
        
        //post内容
        //cmdName
        var postCmdName:String?
        if cmdName != nil {
            
            postCmdName = cmdName
            
            parameters.updateValue(postCmdName!, forKey: NETWORK_COMMAND_KEY_CMD_NAME)
            
        }
        
        
        //cmdValue
        var postCmdValue:String?
        if cmdValue != nil {
            
            postCmdValue = cmdValue
            
            parameters.updateValue(postCmdValue!, forKey: NETWORK_COMMAND_KEY_CMD_VALUE)
            
        }
        
        
        //msgId
        var postMsgId:String?
        if msgId != nil {
            
            postMsgId = msgId
            
            parameters.updateValue(postMsgId!, forKey: NETWORK_COMMAND_KEY_MSG_ID)
            
        }
        
        
        //sendType
        var postSendType:NSNumber?
        if sendType != nil {
            
            postSendType = sendType
            
            parameters.updateValue(postSendType!, forKey: NETWORK_COMMAND_KEY_SEND_TYPE)
            
        }
        
        
        //SerialNumber
        var postSerialNumber = ""
        
        if serialNumber != nil {
            
            postSerialNumber = serialNumber!
            
        }else{
            
            postSerialNumber = UserModel.userModel.selectedDeviceSerialNumber
            
        }
        
        parameters.updateValue(postSerialNumber, forKey: NETWORK_COMMAND_KEY_SERIAL_NUMBER)
        
        
        //id
        var postId:NSNumber = 0
        
        if id != nil {
            
            postId = id!
            
        }else if UserModel.userModel.id != nil{
            
            postId = UserModel.userModel.id!
            
        }
        
        parameters.updateValue(postId, forKey: NETWORK_COMMAND_KEY_ID)
        
        
        
        //clientType
        var postClientType:NSNumber = 0
        
        if clientType != nil {
            
            postClientType = clientType!
            
        }else if UserModel.userModel.clientType != nil{
            
            postClientType = UserModel.userModel.clientType!
            
        }
        
        parameters.updateValue(postClientType, forKey: NETWORK_COMMAND_KEY_CLIENT_TYPE)
        
        
        
        //tokenPass
        var postTokenPass = ""
        
        if tokenPass != nil {
            
            postTokenPass = tokenPass!
            
        }else if UserModel.userModel.tokenPass != nil{
            
            postTokenPass = UserModel.userModel.tokenPass!
            
        }
        
        parameters.updateValue(postTokenPass, forKey: NETWORK_COMMAND_KEY_TOKEN_PASS)
        
        
        
        
        
        let urlString:String = NETWORK_COMMAND_URL_SERVER_URL + NETWORK_COMMAND_URL_PUT_NET_COMMAND
        
        print("urlString: \(urlString), parameters: \(parameters)")
        
        //请求
        Alamofire.request(urlString, method: .post, parameters: parameters).responseJSON { response in
            
            switch response.result{
                
            case .success(let value):
                
                print(value)
                
                success(value)
                
                break
                
            case .failure(let value):
                
                print(failure)
                
                failure(value)
                
                break
                
                
            }
            
        }
        
        
    }
    
    // MARK: - 发送网络指令(OLD) Send Network Command
    ///发送网络指令(OLD) Send Network Command
    class func putChatCommand(commandText:String?, serialNumber:String?, id:NSNumber?, clientType:NSNumber?, tokenPass:String?, success: @escaping (_ responseObject: Any) -> (), failure: @escaping (_ error: Error) -> ()) {
        
        var parameters = [String:Any]()
        
        //post内容
        //commandText
        var postCommandText:String?
        
        if commandText != nil {
            
            postCommandText = commandText
            
            parameters.updateValue(postCommandText!, forKey: NETWORK_COMMAND_KEY_COMMAND_TEXT)
            
        }
        
        
        
        //SerialNumber
        var postSerialNumber = ""
        
        if serialNumber != nil {
            
            postSerialNumber = serialNumber!
            
        }else{
            
            postSerialNumber = UserModel.userModel.selectedDeviceSerialNumber
            
        }
        
        parameters.updateValue(postSerialNumber, forKey: NETWORK_COMMAND_KEY_SERIAL_NUMBER)
        
        
        //id
        var postId:NSNumber = 0
        
        if id != nil {
            
            postId = id!
            
        }else if UserModel.userModel.id != nil{
            
            postId = UserModel.userModel.id!
            
        }
        
        parameters.updateValue(postId, forKey: NETWORK_COMMAND_KEY_ID)
        
        
        
        //clientType
        var postClientType:NSNumber = 0
        
        if clientType != nil {
            
            postClientType = clientType!
            
        }else if UserModel.userModel.clientType != nil{
            
            postClientType = UserModel.userModel.clientType!
            
        }
        
        parameters.updateValue(postClientType, forKey: NETWORK_COMMAND_KEY_CLIENT_TYPE)
        
        
        
        //tokenPass
        var postTokenPass = ""
        
        if tokenPass != nil {
            
            postTokenPass = tokenPass!
            
        }else if UserModel.userModel.tokenPass != nil{
            
            postTokenPass = UserModel.userModel.tokenPass!
            
        }
        
        parameters.updateValue(postTokenPass, forKey: NETWORK_COMMAND_KEY_TOKEN_PASS)
        
        
        
        
        
        let urlString:String = NETWORK_COMMAND_URL_SERVER_URL + NETWORK_COMMAND_URL_PUT_CHAT_COMMAND
        
        print("urlString: \(urlString), parameters: \(parameters)")
        
        //请求
        Alamofire.request(urlString, method: .post, parameters: parameters).responseJSON { response in
            
            switch response.result{
                
            case .success(let value):
                
                print(value)
                
                success(value)
                
                break
                
            case .failure(let value):
                
                print(failure)
                
                failure(value)
                
                break
                
                
            }
            
        }
        
    }
    
    
    // MARK: - 获取所有设备的serialNumber列表
    ///获取所有设备的serialNumber列表
    class func getAllDeviceSerialNumberListString() -> String {
        
        var list:String = ""
        
        if UserModel.userModel.devicesByAccountArray != nil {
            
            if (UserModel.userModel.devicesByAccountArray?.count)! > 0 {
                
                for i in 0 ... (UserModel.userModel.devicesByAccountArray?.count)! - 1 {
                    
                    let dict = UserModel.userModel.devicesByAccountArray?[i]
                    
                    let devicesByAccount = DevicesByAccount.mj_object(withKeyValues: dict)
                    
                    if i == 0 {
                        
                        list = list + (devicesByAccount?.SerialNumber ?? "")
                        
                    }else{
                        
                        list = list + "," + (devicesByAccount?.SerialNumber ?? "")
                        
                    }
                    
                }
                
            }
            
        }
        
        return list
        
    }
    
    // MARK: - 判断选中的serialNumber是否还存在,不在就替换
    ///判断选中的serialNumber是否还存在,不在就替换
    class func autoChangeSelectedSerialNumber() {
        
        let list = NetworkCommand.getAllDeviceSerialNumberListString()
        
        if !list.contains(UserModel.userModel.selectedDeviceSerialNumber) {
            
            UserModel.userModel.selectedDeviceSerialNumber = ""
            
            if (UserModel.userModel.devicesByAccountArray != nil) {
                
                if (UserModel.userModel.devicesByAccountArray?.count)! > 0 {
                    
                    let dict = (UserModel.userModel.devicesByAccountArray?.first)!
                    
                    let devicesByAcoount = DevicesByAccount.mj_object(withKeyValues: dict)
                    
                    UserModel.userModel.selectedDeviceSerialNumber = devicesByAcoount?.SerialNumber ?? ""
                    
                }
                
            }
            
        }
        
    }
    
    // MARK: - 时间戳转String
    ///时间戳转String
    class func timestampToString(timestamp:NSNumber) -> String {
        
        let timeInterval:TimeInterval = TimeInterval.init(timestamp)
        
        let date = Date.init(timeIntervalSince1970: timeInterval)
        
        let dateFormatter = DateFormatter()
        
        dateFormatter.dateFormat="yyyy/MM/dd HH:mm:ss"
        
        let dateString = dateFormatter.string(from: date)
        
        print(dateString)
        
        return dateString
        
    }

}
